import os


class RAMBlockDev:
    def __init__(self, block_size, num_blocks):
        self.block_size = block_size
        self.data = bytearray(block_size * num_blocks)

    def readblocks(self, block_num, buf):
        for i in range(len(buf)):
            buf[i] = self.data[block_num * self.block_size + i]

    def writeblocks(self, block_num, buf):
        for i in range(len(buf)):
            self.data[block_num * self.block_size + i] = buf[i]

    def ioctl(self, op, arg):
        if op == 4: # get number of blocks
            return len(self.data) // self.block_size
        if op == 5: # get block size
            return self.block_size

def demo():
    bdev = RAMBlockDev(512, 50)
    #获取当前路径
    print(os.getcwd())
    #列出当前目录
    a = os.listdir()
    print(a)
    #删除目录
    if 'test' in a :
        os.rmdir('/test')
    #创建新目录
    os.mkdir('/test')


    # #重命名目录
    os.rename('/test', '/test1')
    #获取目录状态
    print(os.stat('test1'))
    #获取文件系统状态
    print(os.statvfs('/'))
    #同步所有文件系统
    os.sync()
    #使用FAT格式化整个闪存
    os.VfsFat.mkfs(bdev)
    #挂载FAT文件系统到根目录
    os.mount(bdev, '/test1')
    print(os.listdir())




if __name__ == '__main__':
    demo()

